package com.smart.lab.view.widget;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import androidx.annotation.Nullable;

public class PictureCropView extends View {

    private Bitmap mBitmap;//图片
    private RectF mCropRect;//裁剪框
    private Paint mPaint;//画笔
    private float mCropX;//裁剪框的X坐标
    private float mCropY;//裁剪框的Y坐标
    private float mCropWidth;//裁剪框的宽度
    private float mCropHeight;//裁剪框的高度
    private float mMaxX;//裁剪框最大X坐标
    private float mMaxY;//裁剪框最大Y坐标
    private float mMinX;//裁剪框最小X坐标
    private float mMinY;//裁剪框最小Y坐标
    private float mOldX;//上一次触摸的X坐标
    private float mOldY;//上一次触摸的Y坐标
    private float mOldCropX;//上一次裁剪框的X坐标
    private float mOldCropY;//上一次裁剪框的Y坐标

    public PictureCropView(Context context) {
        super(context);
        init();
    }

    public PictureCropView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public PictureCropView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        mPaint = new Paint();
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(4);
        mPaint.setColor(Color.WHITE);
    }

    //设置图片
    public void setBitmap(Bitmap bitmap) {
        mBitmap = bitmap;
        mCropX = (getMeasuredWidth() - mBitmap.getWidth()) / 2;
        mCropY = (getMeasuredHeight() - mBitmap.getHeight()) / 2;
        mCropWidth = mBitmap.getWidth();
        mCropHeight = mBitmap.getHeight();
        mMaxX = mCropX + mCropWidth;
        mMaxY = mCropY + mCropHeight;
        mMinX = mCropX;
        mMinY = mCropY;
        invalidate();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (mBitmap != null) {
            //绘制图片
            canvas.drawBitmap(mBitmap, mCropX, mCropY, mPaint);
            //绘制裁剪框
            mCropRect = new RectF(mCropX, mCropY, mCropX + mCropWidth, mCropY + mCropHeight);
            canvas.drawRect(mCropRect, mPaint);
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                mOldX = event.getX();
                mOldY = event.getY();
                mOldCropX = mCropX;
                mOldCropY = mCropY;
                break;
            case MotionEvent.ACTION_MOVE:
                float dx = event.getX() - mOldX;
                float dy = event.getY() - mOldY;
                //保存裁剪框的位置
                mCropX = mOldCropX + dx;
                mCropY = mOldCropY + dy;
                //限制裁剪框的位置
                mCropX = mCropX > mMinX ? mCropX : mMinX;
                mCropY = mCropY > mMinY ? mCropY : mMinY;
                mCropX = mCropX < mMaxX ? mCropX : mMaxX;
                mCropY = mCropY < mMaxY ? mCropY : mMaxY;
                invalidate();
                break;
        }
        return true;
    }

    //裁剪图片
    public Bitmap crop() {
        int width = (int) (mCropX + mCropWidth - mCropX);
        int height = (int) (mCropY + mCropHeight - mCropY);
        Bitmap cropBitmap = Bitmap.createBitmap(mBitmap, (int) mCropX, (int) mCropY, width, height);
        return cropBitmap;
    }
}